<%--
XPTracker - Alternative Agile Tracking Tool
Copyright (C) 2006-2009  Stephen Starkey

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
--%>
<%@ include file="/taglibs.jsp" %>
<c:set var="title" value="Categories" scope="request"/>
<s:layout-render name="/layout/normal.jsp">
  <s:layout-component name="body">
    <script type="text/javascript">
      function addCategory() {
        var name = prompt('Category');
        if (name) {
          document.add.name.value = name;
          document.add.submit();
        }
      }

      function deleteCategory(uuid, name) {
        if (confirm('Really delete category \'' + name + '\'?')) {
          document.deleteForm.uuid.value = uuid;
          document.deleteForm.submit();
        }
      }

      function showUpdateForm(catId) {
        Element.hide($('existingLink'));
        Element.show($('updateImg_' + catId))
      }

      function categoryTypeChanged(catId) {
        $('saveType_' + catId).disabled = false;
        $('revertType_' + catId).disabled = false;
        $('type_' + catId).style.background = 'lightyellow';
      }

      function saveCategoryType(catId) {
        $('saveType_' + catId).disabled = true;
        $('revertType_' + catId).disabled = true;
        $('type_' + catId).disabled = true;

        var params = Form.serialize($('updateType_' + catId));
        new Ajax.Updater('cat_type_holder', $('updateType_' + catId).action,
          {method:'post', postBody: params, onComplete: categorySaved});
      }

      function categorySaved(request) {
        var catId = request.responseText;
        $('type_' + catId).style.background = null;
        $('type_' + catId).disabled = false;
      }

      function revertCategoryType(catId) {
        $('saveType_' + catId).disabled = true;
        $('revertType_' + catId).disabled = true;
        $('type_' + catId).value = originalTypes[catId];
        $('type_' + catId).style.background = null;
      }

      var originalTypes = [];
    </script>

    <div id="cat_type_holder" style="display:none"></div>

    <dt:table name="actionBean.categories" id="category" class="data">
      <dt:column title="Name">${category.name}</dt:column>
      <dt:column title="Icon">
        <a id="existingLink" href="javascript:showUpdateForm(${category.id})">
            <xp:category-image category="${category}" size="large" ifNull="(None)"/>
        </a>
        <s:form beanclass="org.xptracker.web.CategoriesActionBean" style="display:none"
                id="updateImg_${category.id}" enctype="multipart/form-data">
          <s:file name="categoryImage"/>
          <s:hidden name="categoryId" value="${category.id}"/>
          <s:submit name="UpdateImage" value="Update"/>
        </s:form>
      </dt:column>
      <dt:column title="Type">
        <s:form beanclass="org.xptracker.web.CategoriesActionBean" style="display:inline" id="updateType_${category.id}">
          <s:text maxlength="128" size="16" id="type_${category.id}" name="type" value="${category.type}"
                  onkeyup="categoryTypeChanged(${category.id})"/>
          <s:hidden name="categoryId" value="${category.id}"/>
          <input type="hidden" name="UpdateType" value="1"/>
        </s:form>
        <input type="button" id="revertType_${category.id}" value="Undo" disabled="true" onclick="revertCategoryType(${category.id})"/>
        <input type="button" id="saveType_${category.id}" value="Save" disabled="true" onclick="saveCategoryType(${category.id})"/>
        <script type="text/javascript">
          originalTypes[${category.id}] = '${category.type}';
        </script>
      </dt:column>
      <dt:column title="">
        <input type="button" value="Delete" onclick="deleteCategory('${category.id}', '${category.name}')"/>
      </dt:column>
    </dt:table>

    <input type="button" value="Add" onclick="addCategory()"/>

    <s:form beanclass="org.xptracker.web.CategoriesActionBean" style="display:inline" name="add">
      <input type="hidden" name="name"/>
      <input type="hidden" name="Add" value="1"/>
    </s:form>
    <s:form beanclass="org.xptracker.web.CategoriesActionBean" style="display:inline" name="deleteForm">
      <input type="hidden" name="uuid"/>
      <input type="hidden" name="Delete" value="1"/>
    </s:form>

  </s:layout-component>
</s:layout-render>